home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / utilities / text / less-278.lha / less-278 / src.lha / source / optfunc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-02-01  |  8.2 KB  |  502 lines

  1. /*
  2.  * Copyright (c) 1984,1985,1989,1994,1995  Mark Nudelman
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer.
  10.  * 2. Redistributions in binary form must reproduce the above copyright
  11.  *    notice in the documentation and/or other materials provided with 
  12.  *    the distribution.
  13.  *
  14.  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY
  15.  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  16.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
  17.  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE
  18.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
  19.  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 
  20.  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 
  21.  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
  22.  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 
  23.  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN 
  24.  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25.  */
  26.  
  27.  
  28. /*
  29.  * Handling functions for command line options.
  30.  *
  31.  * Most options are handled by the generic code in option.c.
  32.  * But all string options, and a few non-string options, require
  33.  * special handling specific to the particular option.
  34.  * This special processing is done by the "handling functions" in this file.
  35.  *
  36.  * Each handling function is passed a "type" and, if it is a string
  37.  * option, the string which should be "assigned" to the option.
  38.  * The type may be one of:
  39.  *    INIT    The option is being initialized from the command line.
  40.  *    TOGGLE    The option is being changed from within the program.
  41.  *    QUERY    The setting of the option is merely being queried.
  42.  */
  43.  
  44. #include "less.h"
  45. #include "option.h"
  46.  
  47. extern int nbufs;
  48. extern int cbufs;
  49. extern int pr_type;
  50. extern int nohelp;
  51. extern int plusoption;
  52. extern int swindow;
  53. extern int sc_height;
  54. extern int any_display;
  55. extern char *prproto[];
  56. extern char *eqproto;
  57. extern IFILE curr_ifile;
  58. #if LOGFILE
  59. extern char *namelogfile;
  60. extern int force_logfile;
  61. extern int logfile;
  62. extern char *glob();
  63. #endif
  64. #if TAGS
  65. public char *tagoption = NULL;
  66. extern char *tagfile;
  67. extern char *tags;
  68. extern int jump_sline;
  69. #endif
  70. #if MSOFTC
  71. extern int nm_fg_color, nm_bg_color;
  72. extern int bo_fg_color, bo_bg_color;
  73. extern int ul_fg_color, ul_bg_color;
  74. extern int so_fg_color, so_bg_color;
  75. extern int bl_fg_color, bl_bg_color;
  76. #endif
  77.  
  78.  
  79. #if LOGFILE
  80. /*
  81.  * Handler for -o option.
  82.  */
  83.     public void
  84. opt_o(type, s)
  85.     int type;
  86.     char *s;
  87. {
  88.     PARG parg;
  89.  
  90.     switch (type)
  91.     {
  92.     case INIT:
  93.         namelogfile = s;
  94.         break;
  95.     case TOGGLE:
  96.         if (ch_getflags() & CH_CANSEEK)
  97.         {
  98.             error("Input is not a pipe", NULL_PARG);
  99.             return;
  100.         }
  101.         if (logfile >= 0)
  102.         {
  103.             error("Log file is already in use", NULL_PARG);
  104.             return;
  105.         }
  106.         s = skipsp(s);
  107.         namelogfile = glob(s);
  108.         if (namelogfile == NULL)
  109.             namelogfile = save(s);
  110.         use_logfile(namelogfile);
  111.         sync_logfile();
  112.         break;
  113.     case QUERY:
  114.         if (logfile < 0)
  115.             error("No log file", NULL_PARG);
  116.         else
  117.         {
  118.             parg.p_string = namelogfile;
  119.             error("Log file \"%s\"", &parg);
  120.         }
  121.         break;
  122.     }
  123. }
  124.  
  125. /*
  126.  * Handler for -O option.
  127.  */
  128.     public void
  129. opt__O(type, s)
  130.     int type;
  131.     char *s;
  132. {
  133.     force_logfile = TRUE;
  134.     opt_o(type, s);
  135. }
  136. #endif
  137.  
  138. /*
  139.  * Handlers for -l option.
  140.  */
  141.     public void
  142. opt_l(type, s)
  143.     int type;
  144.     char *s;
  145. {
  146.     int err;
  147.     int n;
  148.     char *t;
  149.     
  150.     switch (type)
  151.     {
  152.     case INIT:
  153.         t = s;
  154.         n = getnum(&t, 'l', &err);
  155.         if (err || n <= 0)
  156.         {
  157.             error("Line number is required after -l", NULL_PARG);
  158.             return;
  159.         }
  160.         plusoption = TRUE;
  161.         ungetsc(s);
  162.         break;
  163.     }
  164. }
  165.  
  166. #if USERFILE
  167.     public void
  168. opt_k(type, s)
  169.     int type;
  170.     char *s;
  171. {
  172.     PARG parg;
  173.  
  174.     switch (type)
  175.     {
  176.     case INIT:
  177.         if (lesskey(s))
  178.         {
  179.             parg.p_string = s;
  180.             error("Cannot use lesskey file \"%s\"", &parg);
  181.         }
  182.         break;
  183.     }
  184. }
  185. #endif
  186.  
  187. #if TAGS
  188. /*
  189.  * Handler for -t option.
  190.  */
  191.     public void
  192. opt_t(type, s)
  193.     int type;
  194.     char *s;
  195. {
  196.     IFILE save_ifile;
  197.     POSITION pos;
  198.  
  199.     switch (type)
  200.     {
  201.     case INIT:
  202.         tagoption = s;
  203.         /* Do the rest in main() */
  204.         break;
  205.     case TOGGLE:
  206.         findtag(skipsp(s));
  207.         if (tagfile == NULL)
  208.             break;
  209.         save_ifile = curr_ifile;
  210.         if (edit(tagfile))
  211.             break;
  212.         if ((pos = tagsearch()) == NULL_POSITION)
  213.         {
  214.             if (edit_ifile(save_ifile))
  215.                 quit(QUIT_ERROR);
  216.             break;
  217.         }
  218.         jump_loc(pos, jump_sline);
  219.         break;
  220.     }
  221. }
  222.  
  223. /*
  224.  * Handler for -T option.
  225.  */
  226.     public void
  227. opt__T(type, s)
  228.     int type;
  229.     char *s;
  230. {
  231.     PARG parg;
  232.  
  233.     switch (type)
  234.     {
  235.     case INIT:
  236.         tags = s;
  237.         break;
  238.     case TOGGLE:
  239.         s = skipsp(s);
  240.         tags = glob(s);
  241.         if (tags == NULL)
  242.             tags = save(s);
  243.         break;
  244.     case QUERY:
  245.         parg.p_string = tags;
  246.         error("Tags file \"%s\"", &parg);
  247.         break;
  248.     }
  249. }
  250. #endif
  251.  
  252. /*
  253.  * Handler for -p option.
  254.  */
  255.     public void
  256. opt_p(type, s)
  257.     int type;
  258.     register char *s;
  259. {
  260.     switch (type)
  261.     {
  262.     case INIT:
  263.         /*
  264.          * Unget a search command for the specified string.
  265.          * {{ This won't work if the "/" command is
  266.          *    changed or invalidated by a .lesskey file. }}
  267.          */
  268.         plusoption = TRUE;
  269.         ungetsc(s);
  270.         ungetsc("/");
  271.         break;
  272.     }
  273. }
  274.  
  275. /*
  276.  * Handler for -P option.
  277.  */
  278.     public void
  279. opt__P(type, s)
  280.     int type;
  281.     register char *s;
  282. {
  283.     register char **proto;
  284.     PARG parg;
  285.  
  286.     switch (type)
  287.     {
  288.     case INIT:
  289.     case TOGGLE:
  290.         /*
  291.          * Figure out which prototype string should be changed.
  292.          */
  293.         switch (*s)
  294.         {
  295.         case 'm':  proto = &prproto[PR_MEDIUM];    s++;    break;
  296.         case 'M':  proto = &prproto[PR_LONG];    s++;    break;
  297.         case '=':  proto = &eqproto;        s++;    break;
  298.         default:   proto = &prproto[PR_SHORT];        break;
  299.         }
  300.         free(*proto);
  301.         *proto = save(s);
  302.         break;
  303.     case QUERY:
  304.         parg.p_string = prproto[pr_type];
  305.         error("%s", &parg);
  306.         break;
  307.     }
  308. }
  309.  
  310. /*
  311.  * Handler for the -b option.
  312.  */
  313.     /*ARGSUSED*/
  314.     public void
  315. opt_b(type, s)
  316.     int type;
  317.     char *s;
  318. {
  319.     switch (type)
  320.     {
  321.     case TOGGLE:
  322.     case QUERY:
  323.         /*
  324.          * Allocate the new number of buffers.
  325.          */
  326.         cbufs = ch_nbuf(cbufs);
  327.         break;
  328.     case INIT:
  329.         break;
  330.     }
  331. }
  332.  
  333. /*
  334.  * Handler for the -i option.
  335.  */
  336.     /*ARGSUSED*/
  337.     public void
  338. opt_i(type, s)
  339.     int type;
  340.     char *s;
  341. {
  342.     switch (type)
  343.     {
  344.     case TOGGLE:
  345.         chg_caseless();
  346.         break;
  347.     case QUERY:
  348.     case INIT:
  349.         break;
  350.     }
  351. }
  352.  
  353. /*
  354.  * Handler for the -V option.
  355.  */
  356.     /*ARGSUSED*/
  357.     public void
  358. opt__V(type, s)
  359.     int type;
  360.     char *s;
  361. {
  362.     switch (type)
  363.     {
  364.     case TOGGLE:
  365.     case QUERY:
  366.     case INIT:
  367.         dispversion();
  368.         if (type == INIT)
  369.             quit(QUIT_OK);
  370.         break;
  371.     }
  372. }
  373.  
  374. #if MSOFTC
  375. /*
  376.  *
  377.  */
  378.        static void
  379. colordesc(s, fg_color, bg_color)
  380.     char *s;
  381.     int *fg_color;
  382.     int *bg_color;
  383. {
  384.     int fg, bg;
  385.     int err;
  386.     
  387.     fg = getnum(&s, 'D', &err);
  388.     if (err)
  389.     {
  390.         error("Missing fg color in -D", NULL_PARG);
  391.         return;
  392.     }
  393.     if (*s != '.')
  394.         bg = 0;
  395.     else
  396.     {
  397.         s++;
  398.         bg = getnum(&s, 'D', &err);
  399.         if (err)
  400.         {
  401.             error("Missing fg color in -D", NULL_PARG);
  402.             return;
  403.         }
  404.     }
  405.     *fg_color = fg;
  406.     *bg_color = bg;
  407. }
  408.  
  409. /*
  410.  * Handler for the -D option.
  411.  */
  412.     /*ARGSUSED*/
  413.     public void
  414. opt_D(type, s)
  415.     int type;
  416.     char *s;
  417. {
  418.     switch (type)
  419.     {
  420.     case INIT:
  421.     case TOGGLE:
  422.         switch (*s++)
  423.         {
  424.         case 'n':
  425.             colordesc(s, &nm_fg_color, &nm_bg_color);
  426.             break;
  427.         case 'd':
  428.             colordesc(s, &bo_fg_color, &bo_bg_color);
  429.             break;
  430.         case 'u':
  431.             colordesc(s, &ul_fg_color, &ul_bg_color);
  432.             break;
  433.         case 'k':
  434.             colordesc(s, &bl_fg_color, &bl_bg_color);
  435.             break;
  436.         case 's':
  437.             colordesc(s, &so_fg_color, &so_bg_color);
  438.             break;
  439.         default:
  440.             error("-D must be followed by n, d, u, k or s", NULL_PARG);
  441.             break;
  442.         }
  443.         if (type == TOGGLE)
  444.         {
  445.             so_enter();
  446.             so_exit();
  447.         }
  448.         break;
  449.     case QUERY:
  450.         break;
  451.     }
  452. }
  453. #endif
  454.  
  455. /*
  456.  * "-?" means display a help message.
  457.  * If from the command line, exit immediately.
  458.  */
  459.     /*ARGSUSED*/
  460.     public void
  461. opt_query(type, s)
  462.     int type;
  463.     char *s;
  464. {
  465.     if (nohelp)
  466.         return;
  467.     switch (type)
  468.     {
  469.     case QUERY:
  470.     case TOGGLE:
  471.         error("Use \"h\" for help", NULL_PARG);
  472.         break;
  473.     case INIT:
  474.         /*
  475.          * This is "less -?".
  476.          * It rather ungracefully grabs control, 
  477.          * does the initializations normally done in main,
  478.          * shows the help file and exits.
  479.          */
  480.         raw_mode(1);
  481.         get_term();
  482.         open_getchr();
  483.         init();
  484.         any_display = TRUE;
  485.         help(1);
  486.         quit(QUIT_OK);
  487.         /*NOTREACHED*/
  488.     }
  489. }
  490.  
  491. /*
  492.  * Get the "screen window" size.
  493.  */
  494.     public int
  495. get_swindow()
  496. {
  497.     if (swindow > 0)
  498.         return (swindow);
  499.     return (sc_height + swindow);
  500. }
  501.  
  502.